<?php

if (!defined("BASEPATH"))
    exit("No direct script access allowed");
require_once APPPATH . "controllers/dienhoa-administrator/base_controller" . EXT;

class Khach_hang extends Base_controller {

    var $limit = 20;

    public function __construct() {
        parent::__construct();
        $this->check_login();
    }

    public function tat_ca_kh() {
        $this->permissions_view();
        $data = array();
        //set menu
        $data = $this->SetMenu("KH");

        $total_seg = $this->uri->total_segments();

//config pagination

        if (isset($_POST["btnSend"]) || $this->input->get("pg") === "search") {
            if (isset($_POST["btnSend"])) {
                $TSP = $this->input->post("txtName", TRUE);
            }
            if ($this->input->get("pg") === "search") {
                $TSP = ($this->input->get("name", TRUE)) ? $this->input->get("name", TRUE) : NULL;
            }
            $base_url = base_url() . "dienhoa-administrator/khach_hang/tat_ca_kh/?pg=search&MHD=" . $TSP;
            $result = $this->user_model->GetAllKh($TSP, $this->limit, $this->input->get("page"));
        } else if (!isset($_POST["btnSend"]) || $this->input->get("pg") === "hd") {
            $base_url = base_url() . "dienhoa-administrator/khach_hang/tat_ca_kh/?pg=hd";
            $result = $this->user_model->GetAllKh(NULL,$this->limit, $this->input->get("page"));
        }

        $data["kh"] = $result["rows"]->result();
        $data['num_results'] = $result['num_rows'];
        
//cofig
        $config = array();
        $config['base_url'] = $base_url;
        $config['total_rows'] = $data['num_results'];
        $config['per_page'] = $this->limit;
        $config["page_query_string"] = TRUE;
        $config["query_string_segment"] = "page";
        $this->cfPag();

        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();
        $this->render($this->load->view("admin/khach hang/khach_hang_view", $data, TRUE));
    }

    public function xoa() {
        $this->permission_del_edit();
        $dat = $this->input->post("item");
        for ($i = 0; $i < sizeof($dat); $i++) {
            $delete = $this->user_model->DeleteKH($dat[$i]);
            if ($delete) {
                echo "<script>alert('Không xóa được!'); window.history.back();</script>";
            }
        }
        redirect("dienhoa-administrator/khach_hang/tat_ca_kh", "refresh");
    }

    public function chi_tiet_kh() {
        $this->permissions_view();
        $data = array();
        $data = $this->SetMenu("KH");
        $cid = $this->uri->segment(4);
        $data["kh_view"] = $this->user_model->getUserToID($cid)->row();
        $this->render($this->load->view("admin/khach hang/chi_tiet_kh_view", $data, TRUE));
    }

    public function sua_TtKH() {
        $this->permission_del_edit();
        $cid = $this->uri->segment(4);
        $stt = $this->uri->segment(5);
        $data = array();
        $updateSTT = $this->user_model->UpdateStt($cid, $stt);
        if ($updateSTT)
            echo "<script>window.history.back();</script>";
        else
            echo "<script>alert('khong cap nhat duoc'); window.history.back();</script>";
    }

}

?>
